package pixivPc.task;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import pixivPc.MinMazeException;
import pixivPc.http.RequestUtil;
import pixivPc.http.RequestUtil.SendType;
import pixivPc.img.Img;
import pixivPc.ui.Panel;

/**
 * 登录页面随机图片浏览Task
 * 
 * @author wangm
 *
 */
public class IndexPicTask extends BaseTask {

	/**
	 * 主页url
	 */
	private static final String url = "https://www.pixiv.net";
	/**
	 * 
	 */
	private static final String pattern = "\"landscape\":\\[([^\\]]+)\\]";
	private static final String patternPic = "\\{\"illust_id\":\"(\\d+)\"," + "\"illust_title\":\"([^\"]+)\","
			+ "\"url\":\\{\"medium\":\"([^\"]+)\",\"1200x1200\":\"([^\"]+)\"\\}," + "\"user_name\":\"([^\"]+)\","
			+ "\"profile_img\":\\{\"main_s\":\"([^\"]+)\"\\}," + "\"www_member_illust_medium_url\":\"([^\"]+)\","
			+ "\"www_user_url\":\"([^\"]+)\"\\}";

	/**
	 * 登录页面随机图片浏览Task
	 */
	public IndexPicTask() {
		super(url, "", "", SendType.GET);
	}

	@Override
	protected void process(Panel panel, InputStream in) {
		// TODO Auto-generated method stub
		List<IndexPic> indexPics = new ArrayList<>();
		try {
			String html = RequestUtil.readHtml(in);
			Pattern r = Pattern.compile(pattern);
			Matcher m = r.matcher(html);
			if (m.find()) {
				String trgt = m.group(1);
				Pattern rPic = Pattern.compile(patternPic);
				Matcher mPic = rPic.matcher(trgt);
				while (mPic.find()) {
					indexPics.add(new IndexPic(mPic.group(1), mPic.group(2), mPic.group(3), mPic.group(4),
							mPic.group(5), mPic.group(6), mPic.group(7), mPic.group(8)));
				}
			}
			for (IndexPic indexPic : indexPics) {
				panel.add(indexPic.getButton());
			}

		} catch (IOException e) {
			throw new MinMazeException(e);
		}
	}

	public static class IndexPic {
		private String illustId;
		private String illustTitle;
		private String urlMedium;
		private String url1200;
		private String userName;
		private String profileImg;
		private String wwwMemberIllustMediumUrl;
		private String wwwUserUrl;

		public IndexPic(String illustId, String illustTitle, String urlMedium, String url1200, String userName,
				String profileImg, String wwwMemberIllustMediumUrl, String wwwUserUrl)
				throws UnsupportedEncodingException {
			super();
			this.illustId = illustId;
			this.illustTitle = ascii2native(illustTitle);
			this.urlMedium = urlMedium.replaceAll("\\\\", "");
			this.url1200 = url1200.replaceAll("\\\\", "");
			this.userName = ascii2native(illustTitle);
			this.profileImg = profileImg;
			this.wwwMemberIllustMediumUrl = wwwMemberIllustMediumUrl.replaceAll("\\\\", "");
			this.wwwUserUrl = wwwUserUrl.replaceAll("\\\\", "");
		}

		public JButton getButton() throws IOException {
			System.out.println(urlMedium.replaceAll("\\\\", ""));
			String type = urlMedium.substring(urlMedium.lastIndexOf("."));

			Img img = new Img("tmp\\index", illustTitle + type, urlMedium.replaceAll("\\\\", ""), url);
			img.download();

			ImageIcon icon = new ImageIcon();
			Image image = Toolkit.getDefaultToolkit().getImage("tmp\\index" + File.separator + illustTitle + type);
			icon.setImage(image);

			JButton button = new JButton();
			button.setIcon(icon);
			return button;
		}

	}

	public static String ascii2native(String ascii) {

		List<String> ascii_s = new ArrayList<String>();
		String zhengz = "\\\\u[0-9,a-f,A-F]{4}";
		Pattern p = Pattern.compile(zhengz);
		Matcher m = p.matcher(ascii);
		while (m.find()) {
			ascii_s.add(m.group());
		}
		for (int i = 0, j = 2; i < ascii_s.size(); i++) {
			String code = ascii_s.get(i).substring(j, j + 4);
			char ch = (char) Integer.parseInt(code, 16);
			ascii = ascii.replace(ascii_s.get(i), String.valueOf(ch));
		}
		return ascii;
	}
}
